<?php

namespace app\admin\controller;

use app\admin\common\Common;
use app\admin\model\ShopModel;
use think\Db;
use think\exception\DbException;
use think\facade\Validate;
use think\Request;

class Shop extends LoginThere
{
    public function index()
    {
        try {
            $listShop = Db::name('shop')
                ->alias('s')
                ->join('app a', 'a.appid = s.appid')
                ->field('s.*,a.appname')
                ->paginate(10);
        } catch (DbException $e) {
            return Common::result(400, $e);
        }
        $page = $listShop->render();
        return $this->fetch('/shop/index', ['list' => $listShop, 'page' => $page]);
    }

    public function addshop(Request  $request){
        $data = $request->post();
        $validate = Validate::make([
            'shopname'  => 'require',
            'shoptype' => 'require|number',
            'money' => 'require|number',
            'vipnum' => 'number',
            'inventory' => 'require|number',
            'appid' => 'require|number',
        ]);
        if (!$validate->check($data)) {
            return Common::result(400,$validate->getError());
        }
        return ShopModel::addshop($data);
    }

    public function delshop(Request $request){
        $data = $request->post();
        return ShopModel::delshop($data);
    }

    public function shoporder()
    {
        try {
            $listShoporder = Db::name('shoporder')
                ->alias('s')
                ->join('app a', 'a.appid = s.appid')
                ->field('s.*,a.appname')
                ->paginate(10);
        } catch (DbException $e) {
            return Common::result(400, $e);
        }
        $page = $listShoporder->render();
        return $this->fetch('/shop/shoporder', ['list' => $listShoporder, 'page' => $page]);
    }

    public function delshoporder(Request $request){
        $data = $request->post();
        return ShopModel::delshoporder($data);
    }



}